package study.alex.resumedb.gae;

import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.addon.jpacontainer.provider.CachingMutableLocalEntityProvider;
import com.vaadin.data.Container;
import com.vaadin.data.util.filter.Compare;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import study.alex.resumedb.gae.model.Contact;

import javax.persistence.EntityManager;

/**
 * Created with IntelliJ IDEA.
 * User: alex
 * Date: 27.01.14
 * Time: 14:36
 * SVN $Id$
 */
public class ContactTable extends Table {

    transient EntityManager em;
    private String contactTableSortColumn;
    private boolean contactTableSortAsc;


    public ContactTable(String caption, String userId) {
        super(caption);

        em = JPAContainerFactory.createEntityManagerForPersistenceUnit(EMF.PERSISTENCE_UNIT);
        final JPAContainerForContact contactList = new JPAContainerForContact();
        CachingMutableLocalEntityProvider<Contact> entityProviderContact =
                new CachingMutableLocalEntityProvider<Contact>(Contact.class, em);
        contactList.setEntityProvider(entityProviderContact);
        if (userId != null) {
            Container.Filter userIdFilter = new Compare.Equal("userId", userId);
            contactList.addContainerFilter(userIdFilter);
            contactList.applyFilters();
        }
        contactList.sort(new String[]{"name"}, new boolean[]{true});
        contactTableSortColumn = "name";
        contactTableSortAsc = true;

        setContainerDataSource(contactList);
        setVisibleColumns("name", "organization", "address", "phones", "emails");
        setColumnHeaders(((ResumeDBMaintUI) UI.getCurrent()).getBundle().getString("name"),
                ((ResumeDBMaintUI) UI.getCurrent()).getBundle().getString("organization"),
                ((ResumeDBMaintUI) UI.getCurrent()).getBundle().getString("address"),
                ((ResumeDBMaintUI) UI.getCurrent()).getBundle().getString("phones"),
                ((ResumeDBMaintUI) UI.getCurrent()).getBundle().getString("e.mails"));
        setColumnExpandRatio("name", 2f);
        setColumnExpandRatio("organization", 2f);
        setColumnExpandRatio("address", 2f);
        setColumnExpandRatio("phones", 1.3f);
        setColumnExpandRatio("emails", 1.3f);
        setPageLength(25);
        setSizeFull();
        setColumnReorderingAllowed(true);
        setSelectable(true);
        setMultiSelect(false);
        select(contactList.firstItemId());
        setNullSelectionAllowed(true);
        setImmediate(true);
        setSortEnabled(false);
        addHeaderClickListener(new Table.HeaderClickListener() {
            @Override
            public void headerClick(Table.HeaderClickEvent event) {
                String column = event.getPropertyId().toString();
                if (event.getButton() == MouseEventDetails.MouseButton.LEFT) {
                    if (column.equals("name")) {
                        if (contactTableSortColumn.equals("name")) contactTableSortAsc = !contactTableSortAsc;
                        else {
                            contactTableSortAsc = true;
                            contactTableSortColumn = "name";
                        }
                        sort(new String[]{contactTableSortColumn}, new boolean[]{contactTableSortAsc});
                    } else if (column.equals("organization")) {
                        if (contactTableSortColumn.equals("organization")) contactTableSortAsc = !contactTableSortAsc;
                        else {
                            contactTableSortAsc = true;
                            contactTableSortColumn = "organization";
                        }
                        sort(new String[]{contactTableSortColumn}, new boolean[]{contactTableSortAsc});
                    }
                }
            }
        });
    }
}
